package com.example.myapplication.chap05;

import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class SaveFileActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_05_savefile);
        Button b1 = findViewById(R.id.button_save);
        b1.setOnClickListener(this);
        Button b2 = findViewById(R.id.button_read);
        b2.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button_save) {

//            String fileName="test.txt" ;
//            //保存数据
//            TextView tv = findViewById(R.id.textView6);
//            String content  = tv.getText().toString();
            String fileName = "weather.json";
//            JSONObject sz = new JSONObject();
//
//
//            try {
//                sz.put("city", "深圳");
//                sz.put("date", "15日星期日");
//                sz.put("high", "高温 29℃");
//                sz.put("fx", "无持续风向");
//                sz.put("low", "1低温 25℃");
//                sz.put("type", "中雨");
//            } catch (JSONException e) {
//                throw new RuntimeException(e);
//            }

            Weather weather = new Weather();
            weather.setType("中雨");
            weather.setHigh("高温 29℃");
            weather.setLow("1低温 25℃");
            weather.setFx("无持续风向");
            weather.setCity("深圳");
            weather.setDate("15日星期日");

            Gson gson = new Gson();
            String content = gson.toJson(weather);
//            String content = sz.toString();
//
//            JSONArray array = new JSONArray();
//            array.put(sz);
//            array.put(sz);
//            String content = array.toString();
            FileOutputStream fos;
            try {
                fos = openFileOutput(fileName, MODE_PRIVATE);
                fos.write(content.getBytes()); //将数据写入文件中
                fos.close();  /* 关闭输出流 */
            } catch (Exception e) {
                throw new RuntimeException(e);
            }


            Toast.makeText(this, content, Toast.LENGTH_LONG).show();
        } else if (v.getId() == R.id.button_read) {

            TextView tv = findViewById(R.id.textView6);
            String fileName = "weather.json";
            String content = "";
            FileInputStream fis;
            try {
                fis = openFileInput(fileName); //获得文件输入流对象
                byte[] buffer = new byte[fis.available()];  //创建缓冲区，available()取得文件总字节数
                fis.read(buffer);  //read() 读取数据的下一个字节，可将文件读取到buffer缓冲区
                content = new String(buffer);  //转换成字符串
                fis.close();  //关闭输入流
            } catch (Exception e) {
                e.printStackTrace();
            }

            Gson gson = new Gson();
            Weather w =  gson.fromJson(content,Weather.class);
            TextView t = findViewById(R.id.textView4);
            t.setText("城市：" + w.getCity());
            ((TextView) findViewById(R.id.textView7)).setText("气温：" + w.getHigh());
//
//            try {
//                JSONArray array = new JSONArray(content);
//               // JSONObject js = new JSONObject(content);
//                JSONObject js = array.getJSONObject(0);
//                String city = js.getString("city");
//
//                TextView t = findViewById(R.id.textView4);
//                t.setText("城市：" + city);
//                ((TextView) findViewById(R.id.textView7)).setText("气温：" + js.getString("high"));
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }

//            File SDpath = Environment.getExternalStorageDirectory();
//            tv.setText(SDpath.getAbsolutePath());
            Toast.makeText(this, content, Toast.LENGTH_LONG).show();
        }

    }
}
